package com.socool.sinofaith.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

/**
 * 已提交Fragment
 * @author YD
 *
 */
public class QuestionSubmittedFragment extends Fragment implements
		View.OnClickListener {

	private Button todayFinishButton, yesterdayFinishButton,
			specifiedDateButton;
	private TodayFinishFragment todayFinishFragment;
	private YesterdayFinishFragment yesterdayFinishFragment;
	private SpecifiedDateFragment specifiedDateFragment;
	private FragmentManager fragmentManager;
	private boolean timeset = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_submitted, null);
		SocoolApplication.addActivity(this.getActivity());
		todayFinishButton = (Button) view.findViewById(R.id.todayFinishButton);
		yesterdayFinishButton = (Button) view
				.findViewById(R.id.yesterdayFinishButton);
		specifiedDateButton = (Button) view
				.findViewById(R.id.specifiedDateButton);
		todayFinishButton.setOnClickListener(this);
		yesterdayFinishButton.setOnClickListener(this);
		specifiedDateButton.setOnClickListener(this);
		todayFinishFragment = new TodayFinishFragment();
		yesterdayFinishFragment = new YesterdayFinishFragment();

		fragmentManager = this.getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.replace(R.id.mysubfragcontent, todayFinishFragment);
		transaction.commit();

		return view;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.todayFinishButton:
			todayFinishButton
					.setBackgroundResource(R.drawable.submitted_title_selected);
			yesterdayFinishButton
					.setBackgroundResource(R.drawable.submitted_title_normal);
			specifiedDateButton
					.setBackgroundResource(R.drawable.submitted_title_normal);

			FragmentTransaction transaction1 = fragmentManager
					.beginTransaction();
			transaction1.replace(R.id.mysubfragcontent, todayFinishFragment);
			transaction1.commit();

			// Toast.makeText(this.getActivity(), "今日完成",
			// Toast.LENGTH_LONG).show();
			break;
		case R.id.yesterdayFinishButton:
			todayFinishButton
					.setBackgroundResource(R.drawable.submitted_title_normal);
			yesterdayFinishButton
					.setBackgroundResource(R.drawable.submitted_title_selected);
			specifiedDateButton
					.setBackgroundResource(R.drawable.submitted_title_normal);

			FragmentTransaction transaction2 = fragmentManager
					.beginTransaction();
			transaction2
					.replace(R.id.mysubfragcontent, yesterdayFinishFragment);
			transaction2.commit();

			// Toast.makeText(this.getActivity(), "昨日完成",
			// Toast.LENGTH_LONG).show();
			break;
		case R.id.specifiedDateButton:
			timeset = false;

			todayFinishButton
					.setBackgroundResource(R.drawable.submitted_title_normal);
			yesterdayFinishButton
					.setBackgroundResource(R.drawable.submitted_title_normal);
			specifiedDateButton
					.setBackgroundResource(R.drawable.submitted_title_selected);
			// Toast.makeText(this.getActivity(), "指定日期",
			// Toast.LENGTH_LONG).show();

			// 用来获取日期和时间的
			Calendar calendar = Calendar.getInstance();
			DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker datePicker, int year,
						int month, int dayOfMonth) {
					if (!timeset) {
						String time = year
								+ "-"
								+ ((month + 1) < 10 ? "0" + (month + 1)
										: (month + 1))
								+ "-"
								+ (dayOfMonth < 10 ? "0" + dayOfMonth
										: dayOfMonth);
						Bundle bundle = new Bundle();
						bundle.putString("time", time);
						specifiedDateFragment = new SpecifiedDateFragment();
						specifiedDateFragment.setArguments(bundle);
						FragmentTransaction transaction3 = fragmentManager
								.beginTransaction();
						transaction3.replace(R.id.mysubfragcontent,
								specifiedDateFragment);
						transaction3.commit();
						timeset = true;
					}
				}
			};
			DatePickerDialog datePickerDialog = new DatePickerDialog(
					this.getActivity(), dateListener,
					calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
					calendar.get(Calendar.DAY_OF_MONTH));
			datePickerDialog.show();

			break;
		}
	}

}
